/**
 * Copyright (C) 2012  The CFuture Android SDK Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.toolkit.util;

import android.app.Activity;
import android.content.Intent;

/**
 * @author chenyoca [桥下一粒砂] (chenyoca@163.com)
 * @date 2012-1-13
 * @desc UI工具类
 */
public class ActivitySwitcher {

	public static void switchTo(Activity activity, Class<?> nextUI, String[] keys,Object[] values) {
		Intent intent = new Intent(activity, nextUI);
		if (keys.length != values.length) {
			throw new RuntimeException("Keys参数长度必须与Value参数长度相同！");
		}
		for (int i = 0; i < keys.length; i++) {
			Object val = values[i];
			converType(intent, keys[i], val);
		}
		switchNext(activity,intent);
	}
	
	public static void switchTo(Activity activity, Class<?> nextUI, String key,
			Object value) {
		Intent intent = new Intent(activity, nextUI);
		converType(intent, key, value);
		switchNext(activity,intent);
	}
	
	public static void switchTo(Activity activity, Class<?> nextUI) {
		Intent intent = new Intent(activity, nextUI);
		switchNext(activity,intent);
	}
	
	private static void switchNext(Activity activity,Intent intent){
		activity.startActivity(intent);
		switchAnim(activity);
	}

	/**
	 * Activity切换动画
	 * @param activity 
	 */
//	private static void switchSlide(Activity activity){
//		activity.overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
//	}
	
	/**
	 * Activity切换动画
	 * @param activity 
	 */
	private static void switchAnim(Activity activity){
//		activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
//		activity.overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
	}
	

	/**
	 * 辅助类型转换
	 * @param intent
	 * @param key
	 * @param val
	 */
	private static void converType(Intent intent, String key, Object val) {
		if (val instanceof Boolean)
			intent.putExtra(key, (Boolean) val);
		else if (val instanceof Boolean[])
			intent.putExtra(key, (Boolean[]) val);
		else if (val instanceof String)
			intent.putExtra(key, (String) val);
		else if (val instanceof String[])
			intent.putExtra(key, (String[]) val);
		else if (val instanceof Integer)
			intent.putExtra(key, (Integer) val);
		else if (val instanceof Integer[])
			intent.putExtra(key, (Integer[]) val);
		else if (val instanceof Long)
			intent.putExtra(key, (Long) val);
		else if (val instanceof Long[])
			intent.putExtra(key, (Long[]) val);
		else if (val instanceof Double)
			intent.putExtra(key, (Double) val);
		else if (val instanceof Double[])
			intent.putExtra(key, (Double[]) val);
		else if (val instanceof Float)
			intent.putExtra(key, (Float) val);
		else if (val instanceof Float[])
			intent.putExtra(key, (Float[]) val);

	}

}
